package com.wtb.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;

import com.wtb.rr.domain.RrOauth;


public class RrXmlUtils {

	/**
	 * 写入一条新数据到XML中
	 * 
	 * @param document
	 * @throws IOException
	 */
	public static  Document addToXml(Document document, RrOauth rrOauth){
		Element root = document.getRootElement();
		Element userElement = root.addElement("user");
		userElement.addElement("refresh_token").addText(
				rrOauth.getRefresh_token());
		userElement.addElement("access_token").addText(
				rrOauth.getAccess_token());
		userElement.addElement("expires_in").addText(
				String.valueOf(rrOauth.getExpires_in()));
		userElement.addElement("createTime").addText(
				String.valueOf(rrOauth.getCreateTime()));
		userElement.addElement("uni").addText(
				String.valueOf(rrOauth.getUni()));
		userElement.addElement("ref").addText(
				String.valueOf(rrOauth.getRef()));
		return document;
	}

	/**
	 * 读取所有XML内容 放入HashMap中，便于查找
	 * 
	 * @param document
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, RrOauth> readFromXml(Document document) {
		Map<String, RrOauth> rrOauthMap = new HashMap<String, RrOauth>();
		List<Node> list = document.selectNodes("/users/user");
		if (list != null) {
			for (Node node : list) {
				String refresh_token = node.selectSingleNode("./refresh_token")
						.getStringValue();
				String access_token = node.selectSingleNode("./access_token")
						.getStringValue();
				long expires_in = Long.valueOf(node.selectSingleNode("./expires_in").getStringValue());
				String createTime = node.selectSingleNode("./createTime").getStringValue();
				String uni = node.selectSingleNode("./uni").getStringValue();
				String ref = node.selectSingleNode("./ref").getStringValue();
				RrOauth rrOauth = new RrOauth(access_token, refresh_token,expires_in,uni,createTime,ref);
				rrOauthMap.put(uni, rrOauth);
			}
		}
		return rrOauthMap;
	}

	/**
	 * 修改XML中的一条数据
	 * 
	 * @param document
	 * @param oauthBean
	 * @throws IOException
	 */
	public static  Document modifyXml(Document document, RrOauth rrOauth){
		Element element = (Element) document.selectSingleNode("/users/user/uni[contains(text(),'"+rrOauth.getUni()+"'] ");
		Element refesh = (Element) element.getParent().selectSingleNode(
				"./refresh_token");
		Element access = (Element) element.getParent().selectSingleNode(
				"./access_token");
		Element expires_in = (Element) element.getParent().selectSingleNode(
				"./expires_in");
		Element createTime = (Element) element.getParent().selectSingleNode(
				"./createTime");
		Element uni = (Element) element.getParent().selectSingleNode(
				"./uni");
		Element ref = (Element) element.getParent().selectSingleNode(
				"./ref");
		if (!refesh.getStringValue().equals(rrOauth.getRefresh_token())) {
			refesh.setText(rrOauth.getRefresh_token());
		}
		if (!access.getStringValue().equals(rrOauth.getAccess_token())) {
			access.setText(rrOauth.getAccess_token());
		}
		if (!expires_in.getStringValue().equals(rrOauth.getExpires_in())) {
			expires_in.setText(String.valueOf(rrOauth.getExpires_in()));
		}
		if (!createTime.getStringValue().equals(rrOauth.getCreateTime())) {
			createTime.setText(rrOauth.getCreateTime());
		}
		if (!uni.getStringValue().equals(rrOauth.getUni())) {
			uni.setText(rrOauth.getUni());
		}
		if (!ref.getStringValue().equals(rrOauth.getRef())) {
			ref.setText(rrOauth.getRef());
		}
		return document;
	}

	/**
	 * 删除XML中的一条数据
	 * 
	 * @param document
	 * @param oauthBean
	 * @throws IOException
	 */
	public static  Document delFroXml(Document document, RrOauth rrOauth){
		Element element = (Element) document.selectSingleNode("/users/user/uni[contains(text(),'"+rrOauth.getUni()+"'] ");
		document.getRootElement().remove(element);
		return document;
	}
	

}
